(ns hiccup_plus.components_dirty)



(def according-button-style {:class "accordion-button" :type "button" :data-bs-toggle "collapse" :aria-expanded "true"})

(defn merge-maps-with-flexible-string-concat
  [m1 m2 keys-to-concat]
  (reduce (fn [acc k]
            (let [v1 (get m1 k "")
                  v2 (get m2 k "")
                  concat-val (if (and (string? v1) (string? v2))
                               (str v1 " " v2)
                               (if (string? v1)
                                 v1
                                 (if (string? v2)
                                   v2
                                   "")))]
              (assoc acc k concat-val)))
          (merge m1 m2)
          keys-to-concat))

(defn merge-element [default-style & [args]]
  "合并hccup container央视 class name 为合并 其他为覆盖"
  (let [
        merge-map (reduce conj
                          default-style
                          args)
        maps (filter map? merge-map)
        mm (if (empty? maps)
             {}
             (merge-maps-with-flexible-string-concat (first maps) (last maps) [:class :name])
             ;(reduce into {} maps)
             )
        comps (filter #(not (map? %)) merge-map)
        ]
    (concat (vector (first comps) mm) (vec (rest comps)))
    )
  )
(defn accordion-button
  ([] (accordion-button according-button-style))
  ([style] [:button (merge-maps-with-flexible-string-concat according-button-style style #{:class :id})]))

(defmacro defelem
  [name args & decls]
  `(defn ~(symbol name) [& args#]
     (merge-element ~@(list* decls) args#))
  ;(reduce conj ~@(list* decls) args#))
  )

(defn accordion-header [& args]
  "accordion header"
  [:h2 {:class "accordion-header"} args])

(defn accordion-item [& args]
  "accordion item"
  [:div {:class "accordion-item"}])
;
(defn accordion-collapse [& args]
  [:div {:class "accordion-collapse collapse" :data-bs-parent "#accordionExample"}
   [:div {:class "accordion-body"} args]])
(defn accordion []
  [:div {:class "accordion"}
   (accordion-item (accordion-header (accordion-button)))
   ])
;
(defn alert [type & [args]]
  (reduce conj
          [:div {:class (str "alert alert-" type) :role "alert"}]
          args))

(defmacro define-alert [& names]
  "生成alert对应的函数宏"
  `(do
     ~@(for [name names]
         `(defn ~(symbol (str "alert-" name)) [& args#]
            (alert ~(str name) args#)))))
(define-alert "primary" "secondary" "success" "danger" "warning" "info" "light" "dark")
;
(defmacro defelem
  [name args & decls]
  `(defn ~(symbol name) [& args#]
     (merge-element ~@(list* decls) args#))
  ;(reduce conj ~@(list* decls) args#))
  )

;(defmacro defelem [elements]
;  (def elements [{:name "li-test" :style [:li {:class "list-group-item"}]}])
  ;`(do
  ;   ~@(for [elem elements]
  ;       `(defn ~(symbol (:name elem)) [& args#]
  ;          (merge-element ~(:style elem) args#)
  ;          )
  ;       )
  ;   )
  ;)
(defn badge [color & [args]]
  (reduce
    conj
    [:span {:class (str "badge text-bg-" color)}]
    args)
  )
(defmacro define-badge [& names]
  `(do
     ~@(for [name names]
         `(defn ~(symbol (str "badge-" name)) [& args#]
            (badge ~(str name) args#)))))
(define-badge "primary" "secondary" "success" "danger" "warning" "info" "light" "dark")


(defn button [name value & [args]]
  (reduce conj
          [:button {:type "button" :class (str "btn btn-" name)} value]
          args
          )
  )
(defmacro define-button [& names]
  `(do
     ~@(for [name names]
         `(defn ~(symbol (str "button-" name)) [value# & args#]
            (button ~(str name) value# args#)))))
(define-button "primary" "secondary" "success" "danger" "warning" "info" "light" "dark")




(define-div-class "carousel-inner" "carousel-item" "card-body" "card-title" "card-text" "card-header")

(defelem btn-group [& args]
         [:div {:class "btn-group" :role "group" :aria-label "Basic mixed styles example"}]
         )
(defn bt [va & args]l
  (button "primary" va args))
(defelem breadcrumb [& args]
         [:nav {:aria-label "breadcrumb"} [:ol {:class "breadcrumb"} [:li {:class "breadcrumb-item active" :aria-current "page"}]]]
         )

(defelem ul [& args]
         [:ul {:class "list-group list-group-flush"}]
         )

(defelem li [& args]
         [:li {:class "list-group-item"}])
(defelem card [& args]
         [:div {:class "card" :style "width:18rem;"}])

(defelem card-img [& args]
         [:img {:class "card-img-top"}])
(defelem close-button [& args]
         [:button {:type "button" :class "btn-close" :aria-label "cloase"}])

(defelem list-group [& args]
         [:ul {:class "list-group"}])

(defelem list-group-item [& args]
         [:li {:class "list-group-item"}])

(defmacro define-div-class [& names]
  `(do
     ~@(for [name names]
         `(defelem ~(symbol (str name)) [& args#]
                   [:div {:class ~name}]
                   )))
  )
(define-div-class "carousel-inner" "carousel-item" "card-body" "card-title" "card-text" "card-header")

